//
//  SEGInviteQRCodeViewController.m
//  SEGStandard
//
//  Created by 山刘 on 2019/7/8.
//  Copyright © 2019 山刘. All rights reserved.
//

#import "SEGInviteQRCodeViewController.h"
#import "SEGInviteQRCodeViewModel.h"

#import <MessageUI/MessageUI.h>

@interface SEGInviteQRCodeViewController ()<MFMessageComposeViewControllerDelegate>
@property (nonatomic, strong) UIImageView *qrCodeImageView;
@property (nonatomic, strong) UILabel *titleLable;
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIButton *shareBtn;
@property (nonatomic, strong) UIImageView *bottomIMageView;

@property(nonatomic, strong) SEGInviteQRCodeViewModel *viewModel;
@end

@implementation SEGInviteQRCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = kColor_W;
    self.title = self.title.length > 0  ? self.title : @"邀请邻居";

    [self loadData];
	[self addPageSubviews];
    [self layoutPageSubviews];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

- (void)dealloc {
    NSLog(@"%@-释放了",self.class);
}

-(void)loadData{
}

-(void)addPageSubviews{
    [self.view addSubview:self.backView];
	[self.backView addSubview:self.qrCodeImageView];
    [self.backView addSubview:self.titleLable];
    [self.view addSubview:self.bottomIMageView];
    [self.view addSubview:self.shareBtn];
}

-(void)layoutPageSubviews{
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(270, 287));
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view).offset(61);
    }];
    
    [self.qrCodeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(180, 180));
        make.centerX.equalTo(self.backView);
        make.top.equalTo(self.backView).offset(40);
    }];
    
    [self.titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.backView);
        make.top.equalTo(self.qrCodeImageView.mas_bottom).offset(15);
    }];
    
    [self.shareBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.backView);
        make.top.equalTo(self.backView.mas_bottom).offset(30);
        make.size.mas_equalTo(CGSizeMake(150, 30));
    }];
    
    [self.bottomIMageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(LYScreenWidth(), 210));
        make.left.bottom.equalTo(self.view);
    }];
}

#pragma mark - getter
-(UIImageView *)qrCodeImageView{
    if (!_qrCodeImageView) {
        _qrCodeImageView = [[UIImageView alloc]init];
        _qrCodeImageView.image = [UIImage imageNamed:SEGPicName(@"app_download")];
        _qrCodeImageView.contentMode = UIViewContentModeScaleAspectFill;
        _qrCodeImageView.layer.masksToBounds = YES;
        [_qrCodeImageView sizeToFit];
    }
    return _qrCodeImageView;
}

-(UILabel *)titleLable{
    if (!_titleLable) {
        _titleLable = [[UILabel alloc]init];
        _titleLable.userInteractionEnabled = NO;
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"扫描二维码 下载APP" attributes:@{NSFontAttributeName: [UIFont  systemFontOfSize:13],NSForegroundColorAttributeName: kColor_2}];
        _titleLable.attributedText = attributedString;
    }
    return _titleLable;
}

-(UIView *)backView{
    if (!_backView){
        _backView = [[UIView alloc]init];
        //_backView.frame = CGRectMake(53,124.5,270,286.5);
        _backView.backgroundColor = kColor_W;
        _backView.layer.shadowColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.05].CGColor;
        _backView.layer.shadowOffset = CGSizeMake(0,0);
        _backView.layer.shadowOpacity = 1;
        _backView.layer.shadowRadius = 8;
        _backView.layer.cornerRadius = 5;
    }
    return _backView;
}

-(UIButton *)shareBtn{
    if (!_shareBtn) {
        _shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        _shareBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_shareBtn setTitleColor:kColor_T forState:UIControlStateNormal];
        [_shareBtn setTitle:@"我要分享" forState:UIControlStateNormal];
        
        //[_shareBtn setImage:[UIImage imageNamed:SEGPicName(@“”)] forState:UIControlStateNormal];
        //[_shareBtn setImage:[UIImage imageNamed:SEGPicName(@“”)] forState:UIControlStateHighlighted];
        
        _shareBtn.layer.cornerRadius = 2.0;
        _shareBtn.layer.masksToBounds = YES;
        _shareBtn.layer.borderWidth = 0.5;
        _shareBtn.layer.borderColor = kColor_T.CGColor;
        [_shareBtn addTarget:self action:@selector(shareBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [_shareBtn sizeToFit];
    }
    return  _shareBtn;
}

-(void)shareBtnClick:(UIButton*)sender{
    NSMutableDictionary *tmp = [self.viewModel getSharedParams];
    
    UIViewController *shareVC = [[SEGUhomeMediator sharedInstance] seg_initShareViewController:tmp actionType:^(NSInteger actionType) {
        if (actionType == SEGShareVCActionType_Share_Message) {
            Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
            if (messageClass != nil) {
                if ([messageClass canSendText]) {
                    MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
                    picker.messageComposeDelegate = self;
                    
                    NSString *smsBody = [NSString stringWithFormat:@"你的邻居在邀请你：%@，物业服务一手掌握，快来体验！【%@】", tmp[@"inviteurl"], kAppName];
                    picker.body = smsBody;
                    
                    picker.title = @"";
                    picker.modalPresentationStyle = UIModalPresentationFullScreen;
                    [[[UIApplication sharedApplication] keyWindow].rootViewController presentViewController:picker animated:YES completion:^{
                    }];
                } else {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"设备不支持短信功能" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
                    [alert show];
                }
            }
        }
    } channel:^(NSString *channel) {
    }];
    [self presentViewController:shareVC animated:NO completion:nil];
}

-(UIImageView *)bottomIMageView{
    if (!_bottomIMageView) {
        _bottomIMageView = [[UIImageView alloc]init];
        _bottomIMageView.image = [UIImage imageNamed:SEGPicName(@"img_yaoqing_bg")];
        _bottomIMageView.contentMode = UIViewContentModeScaleAspectFill;
        _bottomIMageView.layer.masksToBounds = YES;
    }
    return _bottomIMageView;
}

#pragma mark - MFMessageComposeViewControllerDelegate
- (void)messageComposeViewController:(MFMessageComposeViewController*)controller didFinishWithResult:(MessageComposeResult)result {
    [controller dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - init Views
-(void)segInitViews{

}

#pragma mark - RAC Data Binding
- (void)bindViewModel {
}

#pragma mark - getter
-(SEGInviteQRCodeViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[SEGInviteQRCodeViewModel alloc]init];
    }
    return _viewModel;
}
@end
